<template>
	<view>
		<view style="margin: 10rpx 20rpx;">位置：
		 <text>{{!isAll?provinceData[defaultP[0]].label+'>>'+cityData[defaultP[0]][defaultP[1]].label:'全国'}}</text><text style="margin-left: 20rpx; text-decoration:underline;color: #008CFF;" @tap="openPicker()">切换</text>
		</view>
			
		<graceAddressPicker :level="2" :show="showPicker" @confirm="confirmPicker" @close="closePicker" :value="defaultP"></graceAddressPicker>
				
		<view style="padding:25rpx 50rpx; background:#F5F6F7">
			<graceSearch @confirm="confirm" :kwd="searchKey"></graceSearch>
		</view>
		
		<view class="searchBotBox">
			<view class="bot_item" v-for="(item,index) in datalist" :key="index" >
				<view class="bot_title"><text class="tleft grace-badge demo grace-bg-blue">{{index+1}}</text><text style="margin-left: 10rpx;">{{item.name}}</text></view>
				<view style="margin-left: 20rpx;" class="grace-tags grace-tbr grace-bg-blue">{{item.category}}</view>
				<view class="bot_desc">地理区域：{{item.area==null||item.area==''?'暂无':item.area}}</view>
				<view class="bot_desc">医疗机构等级：{{item.level==null||item.level==''?'暂无':item.level}}</view>
				<view class="bot_desc" v-if="item.diagnosis_time!=null&&item.diagnosis_time!=''">开诊时间：{{item.diagnosis_time}}</view>
				<view class="bot_desc">电话：{{item.telephone==null||item.telephone==''?'暂无':item.telephone}}</view>
				<view class="bot_desc">地址：{{item.address==null||item.address==''?'暂无':item.address}}</view>
				<view><image @click="goPhone(item)" style="width: 70rpx;height: 70rpx; float: right;margin-top: -100rpx;margin-right: 100rpx;" src="/static/telephone.png"></image></view>
				<view><image @click="goMap(item)" style="width: 70rpx;height: 70rpx; float: right;margin-top: -100rpx;" src="/static/go.png"></image></view>
			</view>
		</view>
		<view style="text-align:center;" v-show="dataShow">数据为空</view>
<!-- 		<view class="entry-mulu" @click="changeMap()">
			<image src="/static/logo.png"></image>
		</view>	 -->
	</view>
</template>
<script>
	import provinceData from '@/graceUI/data/city-data/province.js';
	import cityData from '@/graceUI/data/city-data/city.js';
	import areaData from '@/graceUI/data/city-data/area.js';
	import graceAddressPicker from '@/graceUI/components/graceAddressPicker.vue';
	var page = 1, pageSize = 10;
	import Map from '@/js_sdk/ms-openMap/openMap.js';
	import graceSearch from "@/graceUI/components/graceSearch.vue";
	export default {
		components:{
			graceSearch,
			graceAddressPicker
		},
		data() {
			return {
				type: '',
				dataShow: false,
				datalist: [],
				searchKey : "",
				placeholder: '',
				title: '',
				
				showPicker : false,
				provinceData:provinceData,
				cityData:cityData,
				defaultP : [0,0,0],
				isAll: true,
			}
		},
		onUnload(e){
			page = 1;
			this.datalist = [];
		},
		onLoad(e) {
			if(e.type!=null && e.type!=''){
				if(e.type==0){
					this.title = '全国医疗救治发热门诊';
				}else if(e.type==1){
					this.title = '全国医疗救治定点医院';
				}else if(e.type==''){
					this.title = '全国医疗救治发热门诊与定点医院';
				}
				this.type = e.type;
				uni.setNavigationBarTitle({
				　　title: this.title
				})
			}
			if(e.searchKey!=null && e.searchKey!=''){
				this.searchKey = e.searchKey;
				this.isAll = true;
			}
			if(e.pindex!=null && e.cindex!=null){
				this.defaultP =  [Number(e.pindex),Number(e.cindex),0];
				this.isAll = false;
			}
			page = 1;
			this.datalist = [];
			this.getHospitals('refresh');
		},
		//下拉刷新
		onPullDownRefresh : function(){
			this.getHospitals('refresh');
			// console.log("下拉刷新");
		},
		// 加载更多
		onReachBottom : function(){
			this.getHospitals('add');
			// console.log("上拉加载");
		},
		methods: {
			openPicker : function(){
				this.showPicker = true;
			},
			confirmPicker:function (e) {
				// console.log(e);
				// this.address = e.names[0]+'>>'+e.names[1]+'>>'+e.names[2];
				this.defaultP =  e.indexs;
				this.isAll = false;
				this.getHospitals('refresh');
				this.closePicker();
			},
			closePicker : function(){
				this.showPicker = false;
			},
			
			confirm : function (e) {
				this.searchKey = e;
				this.getHospitals('refresh');
			},
			getHospitals(t){
				if(t=='refresh'){
					page = 1;
					this.datalist=[];
				}
				uni.request({
					url: this.serverUrl + '/feiyan/search-api/getHospitals',
					header: {
						"content-type": "application/x-www-form-urlencoded; charset=UTF-8"
					},
					data: {
						province: !this.isAll?this.provinceData[this.defaultP[0]].label:'',
						city: !this.isAll?this.cityData[this.defaultP[0]][this.defaultP[1]].label!='市辖区'?this.cityData[this.defaultP[0]][this.defaultP[1]].label:'':'',
						district: '',
						name: this.searchKey,
						type: '3',//this.type,
						pageNumber: page,
						pageSize: pageSize
					},
					method: "POST",
					success: (r) => {
						// console.log(r);
						if (r.data.code == '000000') {
							var count = r.data.data.count;
							var pageSum = Math.ceil(count/pageSize);
							if(pageSum==0){
								this.dataShow = true;
							}else{
								if (page>pageSum) {
									uni.showToast({
										title: "已经加载全部",
										icon: "none",
										duration: 1500
									});
								}else{
									this.dataShow = false;
									for(var i=0;i<r.data.data.data.length;i++){
										var tempobj = {
											name: r.data.data.data[i].name,
											level: r.data.data.data[i].level,
											address: r.data.data.data[i].address,
											telephone: r.data.data.data[i].telephone,
											map_point: r.data.data.data[i].map_point,
											province: r.data.data.data[i].province,
											city: r.data.data.data[i].city,
											time: r.data.data.data[i].diagnosis_time,
											type: r.data.data.data[i].type,
											category: r.data.data.data[i].category,
											area: r.data.data.data[i].area,
											diagnosis_time: r.data.data.data[i].diagnosis_time,
										};
										this.datalist = this.datalist.concat(tempobj);
									}
									page++;
								}
							}
						} else {
							uni.showToast({
								title: r.data.message
							});
						}
					},
					complete: r => {
						uni.stopPullDownRefresh();
					}
				})
			},
			
			goMap(item){
				if(item.map_point==null || item.map_point==''){
					uni.showToast({
						icon:'none',
						title:'暂无位置信息'
					})
					return;
				}
				var xpoint = Number(item.map_point.split(',')[0]);
				var ypoint = Number(item.map_point.split(',')[1]);
				var pname = item.name;
				Map.openMap(xpoint, ypoint, pname, 'wgs84');
			},
			goPhone(item){
				if(item.telephone==null || item.telephone==''){
					uni.showToast({
						icon:'none',
						title:'暂无联系方式'
					})
					return;
				}
				uni.makePhoneCall({
				    phoneNumber: item.telephone.split('；|;')[0].split('/')[0].split('(')[0]
				});
			}
			
		}
	}
</script>
<style lang="scss">
	page {
		background: #FFF;
	}

	.bot_item{
		margin: 10rpx 20rpx;
		padding: 10rpx 10rpx;
	}
	.bot_title{
		margin-left: 20rpx;
		font-size: 28rpx;
		line-height: 60rpx;
	}
	.bot_desc{
		margin-left: 20rpx;
		font-size: 24rpx;
		line-height: 50rpx;
		color: #AAA9AB;
		width: 70%;
	}
	
	.entry-mulu {
		position: fixed;
		bottom: 5%;
		right: 8%;
		z-index: 888;
		/* background: #ff9900; */
		width: 90upx;
		height: 90upx;
		/* border-radius: 40px;
		box-shadow: 2px 2px 2px #888888;
		opacity:0.7 ; */
	}
	
	.entry-mulu image {
		padding: 8upx;
		width: 90upx;
		height: 90upx;
	}
</style>
